package com.zhang.zhangapp;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.GuideFragment;
import com.zhang.zhangapp.view.adapter.FragmentAdapter;
import com.zhang.zhangapp.view.custom.IndicatorView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/1.
 */
@ContentView(R.layout.acitivity_guide)
public class GuideActivity extends FragmentActivity {

    @ViewInject(R.id.guide_ad_viewpager)
    ViewPager viewPager;
    @ViewInject(R.id.guide_indicator)
    IndicatorView indicatorView;

    FragmentAdapter adapter;
    List<Fragment> fragmentList =new ArrayList<>();

    //所需要申请的权限数组
    private static final String[] permissionsArray = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    //还需申请的权限列表
    private List<String> permissionsList = new ArrayList<String>();
    //申请权限后的返回码
    private static final int REQUEST_CODE_ASK_PERMISSIONS = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initViewPager();
        checkRequiredPermission(this);
    }

    private void initViewPager(){
        GuideFragment fragment1 = new GuideFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("sourceImage",R.mipmap.guid1);
        fragment1.setArguments(bundle);

        GuideFragment fragment2 = new GuideFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putInt("sourceImage",R.mipmap.guid2);
        fragment2.setArguments(bundle2);

        fragmentList.add(fragment1);
        fragmentList.add(fragment2);

        adapter = new FragmentAdapter(getSupportFragmentManager(),fragmentList);

        viewPager.setAdapter(adapter);
        indicatorView.setViewPager(fragmentList.size(),viewPager);
    }

    private void checkRequiredPermission(final Activity activity) {
        for (String permission : permissionsArray) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionsList.add(permission);
            }
        }
        ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_PERMISSIONS);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {

                    } else {
                    }
                }
                MApplication.getInstance().initLocation();
                MApplication.getInstance().initDB();
                break;
        }
        //  checkIsFirstOpenApp();
    }
}
